//
//  LXPlanCollectionView.m
//  ZuLinProject
//
//  Created by bancheng on 2022/9/15.
//  Copyright © 2022 LX_App. All rights reserved.
//

#import "LXLoanPlanRepaymentView.h"
#import "LXLoanModel.h"
@interface LXLoanPlanRepaymentView()

@end
@implementation LXLoanPlanRepaymentView


-(void)awakeFromNib{
    [super awakeFromNib];
    [self.backView addCorner:5];
    
    
    
    [self.cancelBtn addCorner:3 AndLine:ColorWith3C6CFC];
    [self.confirmBtn addCorner:3];
    self.amountTextField.delegate = self;
    self.remarkTextField.delegate = self;
    [self.closeBtn whenTapped:^{
        [self removeFromSuperview];
    }];
    [self.cancelBtn whenTapped:^{
        [self removeFromSuperview];
    }];
    
    
   
    
    // 收款日期
    [self.paymentDateView whenTapped:^{
        [self endEditing:YES];
        DatePickView *datePicker =[[DatePickView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, ScreenHeight) withDateStr:self.paymentDateLab.text withIsMaxDate:YES withNoForward:NO WithTips:@"请选择应收日期"] ;
        [datePicker showBoomView];
        [[[UIApplication sharedApplication].delegate window] addSubview:datePicker];
        
        datePicker.block = ^(NSString *yearString){
            self.paymentDateLab.text = yearString;
            self.model.exceptPaidDate = yearString;
        };
        
        
    }];
    
}
- (void)setModel:(LXLoanModel *)model{
    _model = model;
    
    self.periodsTextField.text = model.period;
    self.paymentDateLab.text = model.exceptPaidDate;
    self.amountTextField.text = model.amount;
    self.remarkTextField.text = model.remark;
    
    
}




- (void)textFieldDidEndEditing:(UITextField *)textField{
//    if (textField == self.amountTextField) {
//        self.model.amount = textField.text;
//    }
//
//    else {
//        self.model.remark = textField.text;
//    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    NSString *shouldText = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    if (textField == self.amountTextField) {
        NSInteger maxLength = 8;
        NSInteger strLength = shouldText.length;
        
        if (strLength > maxLength) {
            // 允许删除
            if (strLength < textField.text.length) {
                return YES;
            }
            
            return NO;
        }
        
        
        return [NSString validateNumberNegativePoint:shouldText];
     
    }

    
    return YES;
    
}

- (IBAction)confirmAction:(UIButton *)sender {
    

    if (StringIsEmpty(self.amountTextField.text)) {
        showAlert(@"请输入应付金额");
        return;
    }
    
//    if (textField == self.amountTextField) {
        self.model.amount = self.amountTextField.text;
//    }

//    else {
        self.model.remark = self.remarkTextField.text;
//    }
    
    
    if (self.clickPlanAction) {
        
        self.clickPlanAction(self.model);
    }

    [self removeFromSuperview];
   
}

@end
